home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / cpp / cpp3.c < prev    next >
C/C++ Source or Header  |  1992-04-13  |  14KB  |  531 lines

  1. /*
  2.  
  3.  
  4.  Copyright (C) 1990 Texas Instruments Incorporated.
  5.  
  6.  Permission is granted to any individual or institution to use, copy, modify,
  7.  and distribute this software, provided that this complete copyright and
  8.  permission notice is maintained, intact, in all copies and supporting
  9.  documentation.
  10.  
  11.  Texas Instruments Incorporated provides this software "as is" without
  12.  express or implied warranty.
  13.  
  14.  
  15.  *                C P P 3 . C
  16.  *
  17.  *            File open and command line options
  18.  *
  19.  * Edit history
  20.  * 13-Nov-84    MM    Split from cpp1.c
  21.  * 21-Oct-85    rms    Make -g command arg not cause an error.
  22.  * 24-Sep-89    AFM     OS2, XENIX and AIX support.
  23.  * 19-Jan-90    DKM     MVS support.
  24.  * 18-May-90    MBN     Conditional compilation for COOL to get "clean" cpp
  25.  * 16-Jun-91    GPD     Added support for Sun -Y option
  26.  *             Added support for Sun -undef option
  27.  * 01-Jul-91    GPD    Add __COOL__ as a predefined symbol.
  28.  * 07-Jul-91    GPD    Fix #line nesting. Force sharp() before processing
  29.  *            new file if wrongline.
  30.  */
  31.  
  32.  
  33. #include    <stdio.h>
  34. #include    <ctype.h>
  35. #include    <time.h>
  36. #include    "cppdef.h"
  37. #include    "cpp.h"
  38.  
  39. /* The type returned from time is double on MVS, long everywhere else.
  40.  * MVS defines this type as time_t.   Add non MVS definition here.
  41.  */
  42. #if HOST != SYS_MVS
  43. #define  time_t long
  44. #endif
  45.  
  46. #if DEBUG 
  47. #if (HOST == SYS_VMS || HOST == SYS_UNIX || HOST == SYS_XENIX || HOST == SYS_OS2 || HOST == SYS_AIX)
  48. #include    <signal.h>
  49. extern int    abort();        /* For debugging        */
  50. #endif
  51. #endif
  52.  
  53. extern void expand_line();
  54. extern void expand_file();
  55.  
  56. #ifdef COOL
  57. extern void define_package();       /* Define a new symbol package */
  58. #endif
  59.  
  60. int
  61. openfile(filename)
  62. char        *filename;
  63. /*
  64.  * Open a file, add it to the linked list of open files.
  65.  * This is called only from doinclude().
  66.  */
  67. {
  68.     register FILE        *fp;
  69.  
  70.     if ((fp = fopen(filename, "r")) == NULL) {
  71. #if DEBUG
  72.         perror(filename);
  73. #endif
  74.         return (FALSE);
  75.     }
  76. #if DEBUG
  77.     if (debug)
  78.         fprintf(stderr, "Reading from \"%s\"\n", filename);
  79. #endif
  80.     if (wrongline)
  81.       sharp();
  82.     addfile(fp, filename);
  83.     return (TRUE);
  84. }
  85.  
  86. addfile(fp, filename)
  87. FILE        *fp;            /* Open file pointer        */
  88. char        *filename;        /* Name of the file        */
  89. /*
  90.  * Initialize tables for this open file.  This is called from openfile()
  91.  * above (for #include files), and from the entry to cpp to open the main
  92.  * input file.  It calls a common routine, getfile() to build the FILEINFO
  93.  * structure which is used to read characters.  (getfile() is also called
  94.  * to setup a macro replacement.)
  95.  */
  96. {
  97.     register FILEINFO    *file;
  98.     extern FILEINFO        *getfile();
  99.  
  100.     file = getfile(NBUFF, filename);
  101.     file->fp = fp;            /* Better remember FILE *    */
  102.     file->buffer[0] = EOS;        /* Initialize for first read    */
  103.     line = 1;            /* Working on line 1 now    */
  104.     wrongline = TRUE;        /* Force out initial #line    */
  105. }
  106.  
  107. setincdirs()
  108. /*
  109.  * Append system-specific directories to the include directory list.
  110.  * Called only when cpp is started.
  111.  */
  112. {
  113.  
  114. #ifdef    CPP_INCLUDE
  115.     *incend++ = CPP_INCLUDE;
  116. #define    IS_INCLUDE    1
  117. #else
  118. #define    IS_INCLUDE    0
  119. #endif
  120.  
  121. #if (HOST == SYS_UNIX || HOST == SYS_XENIX || HOST == SYS_AIX)
  122.     if (yflag && altincl)
  123.         *incend++ = altincl;
  124.     else
  125.         *incend++ = "/usr/include";
  126. #define    MAXINCLUDE    (NINCLUDE - 1 - IS_INCLUDE)
  127. #endif
  128.  
  129. #if HOST == SYS_OS2
  130.     *incend++ = "\\ibmc2\\include";
  131. #define    MAXINCLUDE    (NINCLUDE - 1 - IS_INCLUDE)
  132. #endif
  133.  
  134. #if HOST == SYS_MVS
  135. /* MVS will not use the include list via the -I option.  Instead
  136.  * logical ddnames are allocated to a concatenation of include
  137.  * libraries.  Specifying -I for mvs will be an error.
  138.  */
  139. #define    MAXINCLUDE    0
  140. #endif
  141.  
  142. #if HOST == SYS_VMS
  143.     extern char    *getenv();
  144.  
  145.     if (getenv("C$LIBRARY") != NULL)
  146.         *incend++ = "C$LIBRARY:";
  147.     *incend++ = "SYS$LIBRARY:";
  148. #define    MAXINCLUDE    (NINCLUDE - 2 - IS_INCLUDE)
  149. #endif
  150.  
  151. #if HOST == SYS_RSX
  152.     extern int    $$rsts;            /* TRUE on RSTS/E    */
  153.     extern int    $$pos;            /* TRUE on PRO-350 P/OS    */
  154.     extern int    $$vms;            /* TRUE on VMS compat.    */
  155.  
  156.     if ($$pos) {                /* P/OS?        */
  157.         *incend++ = "SY:[ZZDECUSC]";    /* C #includes        */
  158.         *incend++ = "LB:[1,5]";        /* RSX library        */
  159.     }
  160.     else if ($$rsts) {            /* RSTS/E?        */
  161.         *incend++ = "SY:@";            /* User-defined account    */
  162.         *incend++ = "C:";            /* Decus-C library    */
  163.         *incend++ = "LB:[1,1]";        /* RSX library        */
  164.     }
  165.     else if ($$vms) {            /* VMS compatibility?    */
  166.         *incend++ = "C:";
  167.     }
  168.     else {                    /* Plain old RSX/IAS    */
  169.         *incend++ = "LB:[1,1]";
  170.     }
  171. #define    MAXINCLUDE    (NINCLUDE - 3 - IS_INCLUDE)
  172. #endif
  173.  
  174. #if HOST == SYS_RT11
  175.     extern int    $$rsts;            /* RSTS/E emulation?    */
  176.  
  177.     if ($$rsts)
  178.         *incend++ = "SY:@";            /* User-defined account    */
  179.     *incend++ = "C:";            /* Decus-C library disk    */
  180.     *incend++ = "SY:";            /* System (boot) disk    */
  181. #define    MAXINCLUDE    (NINCLUDE - 3 - IS_INCLUDE)
  182. #endif
  183. }
  184.  
  185. int
  186. dooptions(argc, argv)
  187. int        argc;
  188. char        *argv[];
  189. /*
  190.  * dooptions is called to process command line arguments (-Detc).
  191.  * It is called only at cpp startup.
  192.  */
  193. {
  194.     register char        *ap;
  195.     register DEFBUF        *dp;
  196.     register int        c;
  197.     int            i, j;
  198.     char            *arg;
  199.     SIZES            *sizp;        /* For -S        */
  200.     int            size;        /* For -S        */
  201.     int            isdatum;    /* FALSE for -S*    */
  202.     int            endtest;    /* For -S        */
  203.  
  204.     for (i = j = 1; i < argc; i++) {
  205.         arg = ap = argv[i];
  206.         if (*ap++ != '-' || *ap == EOS)
  207.         argv[j++] = argv[i];
  208.         else {
  209.         c = *ap++;            /* Option byte        */
  210.         if (islower(c))            /* Normalize case    */
  211.             c = toupper(c);
  212.         switch (c) {            /* Command character    */
  213.         case 'C':            /* Keep comments    */
  214.             cflag = TRUE;
  215.             keepcomments = TRUE;
  216.             break;
  217.  
  218.         case 'D':            /* Define symbol    */
  219. #if (HOST != SYS_UNIX && HOST != SYS_XENIX && HOST != SYS_OS2 && HOST != SYS_MVS && HOST != SYS_AIX)
  220.             zap_uc(ap);            /* Force define to U.C.    */
  221. #endif
  222.             /*
  223.              * If the option is just "-Dfoo", make it -Dfoo=1
  224.              */
  225.             while (*ap != EOS && *ap != '=')
  226.             ap++;
  227.             if (*ap == EOS)
  228.             ap = "1";
  229.             else
  230.             *ap++ = EOS;
  231.             /*
  232.              * Now, save the word and its definition.
  233.              */
  234.             dp = defendel(argv[i] + 2, FALSE);
  235.             dp->repl = savestring(ap);
  236.             dp->nargs = DEF_NOARGS;
  237.             break;
  238.  
  239.         case 'E':            /* Ignore non-fatal    */
  240.             eflag = TRUE;        /* errors.        */
  241.             break;
  242.  
  243.         case 'G':            /* Cmpiler's debug switch */
  244.             break;
  245.  
  246.         case 'B':            /* Sun's ignore // comments */
  247.             break;
  248.  
  249.         case 'I':            /* Include directory    */
  250.             if (incend >= &incdir[MAXINCLUDE])
  251.             cfatal("Too many include directories", NULLST);
  252.             *incend++ = ap;
  253.             break;
  254.  
  255.         case 'N':            /* No predefineds    */
  256.             nflag++;            /* Repeat to undefine    */
  257.             break;            /* __LINE__, etc.    */
  258.  
  259.         case 'S':
  260.             sizp = size_table;
  261.             if (isdatum = (*ap != '*'))    /* If it's just -S,    */
  262.             endtest = T_FPTR;    /* Stop here        */
  263.             else {            /* But if it's -S*    */
  264.             ap++;            /* Step over '*'    */
  265.             endtest = 0;        /* Stop at end marker    */
  266.             }
  267.             while (sizp->bits != endtest && *ap != EOS) {
  268.             if (!isdigit(*ap)) {    /* Skip to next digit    */
  269.                 ap++;
  270.                 continue;
  271.             }
  272.             size = 0;        /* Compile the value    */
  273.             while (isdigit(*ap)) {
  274.                 size *= 10;
  275.                 size += (*ap++ - '0');
  276.             }
  277.             if (isdatum)
  278.                 sizp->size = size;    /* Datum size        */
  279.             else
  280.                 sizp->psize = size;    /* Pointer size        */
  281.             sizp++;
  282.             }
  283.             if (sizp->bits != endtest)
  284.             cwarn("-S, too few values specified in %s", argv[i]);
  285.             else if (*ap != EOS)
  286.             cwarn("-S, too many values, \"%s\" unused", ap);
  287.             break;
  288.  
  289.         case 'X':            /* Debug        */
  290.             debug = (isdigit(*ap)) ? atoi(ap) : 1;
  291. #if DEBUG
  292. #if (HOST == SYS_VMS || HOST == SYS_UNIX || HOST == SYS_XENIX || HOST == SYS_OS2 || HOST == SYS_AIX)
  293.             signal(SIGINT, abort);    /* Trap "interrupt"    */
  294. #endif
  295. #endif
  296.             fprintf(stderr, "Debug set to %d\n", debug);
  297.             break;
  298.  
  299.         case 'Y':
  300.             altincl = ap;
  301.             yflag = TRUE;
  302.             break;
  303.  
  304.         case 'U':            /* Undefine symbol    */
  305. #if (HOST != SYS_UNIX && HOST != SYS_XENIX && HOST != SYS_OS2 && HOST != SYS_MVS && HOST != SYS_AIX)
  306.             zap_uc(ap);
  307. #endif
  308.             if (strcmp(ap-1,"undef") == 0)
  309.             {
  310.             if (nflag == 0)
  311.                 nflag = 1;
  312.             break;
  313.             }
  314.             if (defendel(ap, TRUE) == NULL)
  315.             cwarn("\"%s\" wasn't defined", ap);
  316.             break;
  317.  
  318.         default:            /* What is this one?    */
  319.             cwarn("Unknown option \"%s\"", arg);
  320.             fprintf(stderr, "The following options are valid:\n\
  321.   -C\t\t\tWrite source file comments to output\n\
  322.   -Dsymbol=value\tDefine a symbol with the given (optional) value\n\
  323.   -Idirectory\t\tAdd a directory to the #include search list\n\
  324.   -N\t\t\tDon't predefine target-specific names\n\
  325.   -Stext\t\tSpecify sizes for #if sizeof\n\
  326.   -Usymbol\t\tUndefine symbol\n\
  327.   -Xvalue\t\tSet internal debug flag\n");
  328.             break;
  329.         }            /* Switch on all options    */
  330.         }                /* If it's a -option        */
  331.     }                /* For all arguments        */
  332.     if (j > 3) {
  333.         cerror(
  334.         "Too many file arguments.  Usage: cpp [input [output]]",
  335.         NULLST);
  336.     }
  337.     return (j);            /* Return new argc        */
  338. }
  339.  
  340. #if (HOST != SYS_UNIX && HOST != SYS_XENIX && HOST != SYS_OS2 && HOST != SYS_MVS && HOST != SYS_AIX)
  341. FILE_LOCAL
  342. zap_uc(ap)
  343. register char    *ap;
  344. /*
  345.  * Dec operating systems mangle upper-lower case in command lines.
  346.  * This routine forces the -D and -U arguments to uppercase.
  347.  * It is called only on cpp startup by dooptions().
  348.  */
  349. {
  350.     while (*ap != EOS) {
  351.         /*
  352.          * Don't use islower() here so it works with Multinational
  353.          */
  354.         if (*ap >= 'a' && *ap <= 'z')
  355.         *ap = toupper(*ap);
  356.         ap++;
  357.     }
  358. }
  359. #endif
  360.  
  361. define_builtin (name, function, replacement)
  362.      char *name;
  363.      void (*function)();
  364.      char *replacement;
  365. {
  366.   DEFBUF *dp;
  367.   dp = defendel(name, FALSE);
  368.   dp->repl = replacement;
  369.   dp->expander = function;
  370.   dp->nargs = (function == NULL) ? DEF_NOARGS : DEF_BUILTIN;
  371. }
  372.  
  373. static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
  374.                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",};
  375.  
  376. initdefines()
  377. /*
  378.  * Initialize the built-in #define's.  There are two flavors:
  379.  *     #xomdefine decus    1        (static definitions)
  380.  *    #define    __FILE__ ??        (dynamic, evaluated by magic)
  381.  * Called only on cpp startup.
  382.  *
  383.  * Note: the built-in static definitions are supressed by the -N option.
  384.  * __LINE__, __FILE__, and __DATE__ are always present.
  385.  */
  386. {
  387.     register char        **pp;
  388.     register char        *tp;
  389.     register DEFBUF        *dp;
  390.     struct tm        *tvec;
  391.     /*
  392.      * Predefine the built-in symbols.  Allow the
  393.      * implementor to pre-define a symbol as "" to
  394.      * eliminate it.
  395.      */
  396.     if (nflag == 0) {
  397.         for (pp = preset; *pp != NULL; pp++) {
  398.         if (*pp[0] != EOS) {
  399.             dp = defendel(*pp, FALSE);
  400.             dp->repl = savestring("1");
  401.             dp->nargs = DEF_NOARGS;
  402.         }
  403.         }
  404.     }
  405.  
  406.     if (nflag < 2) {
  407.  
  408.       define_builtin("__LINE__", expand_line, "");
  409.       define_builtin("__FILE__", expand_file, "");
  410.       define_builtin("__STDC__", NULL, "1");
  411. #ifdef COOL
  412.       define_builtin("__COOL__", NULL, "1");
  413. #endif
  414. #if OK_DATE
  415.       /*
  416.        * Define __DATE__ as today's date.
  417.        */
  418.       { time_t clock = time(NULL);
  419.         tvec = localtime(&clock);}
  420.       tp = getmem(12);
  421.       /* strftime(tp, 12, "%b %d %Y", tp); */
  422.       sprintf(tp, "%s %02d %04d",
  423.           months[tvec->tm_mon],
  424.           tvec->tm_mday,
  425.           tvec->tm_year + 1900);
  426.       define_builtin( "__DATE__", NULL, tp);
  427.  
  428.       tp = getmem(9);
  429.       /* strftime(tp, 9, "%H:%M:%S", tp); */
  430.       sprintf(tp, "%02d:%02d:%02d",
  431.           tvec->tm_hour,
  432.           tvec->tm_min,
  433.           tvec->tm_sec);
  434.       define_builtin( "__TIME__", NULL, tp);
  435. #endif
  436. #ifdef COOL
  437.       define_builtin("DEFPACKAGE", define_package, "");
  438. #endif
  439.     }
  440. }
  441.  
  442. #if HOST == SYS_VMS
  443. /*
  444.  * getredirection() is intended to aid in porting C programs
  445.  * to VMS (Vax-11 C) which does not support '>' and '<'
  446.  * I/O redirection.  With suitable modification, it may
  447.  * useful for other portability problems as well.
  448.  */
  449.  
  450. int
  451. getredirection(argc, argv)
  452. int        argc;
  453. char        **argv;
  454. /*
  455.  * Process vms redirection arg's.  Exit if any error is seen.
  456.  * If getredirection() processes an argument, it is erased
  457.  * from the vector.  getredirection() returns a new argc value.
  458.  *
  459.  * Warning: do not try to simplify the code for vms.  The code
  460.  * presupposes that getredirection() is called before any data is
  461.  * read from stdin or written to stdout.
  462.  *
  463.  * Normal usage is as follows:
  464.  *
  465.  *    main(argc, argv)
  466.  *    int        argc;
  467.  *    char        *argv[];
  468.  *    {
  469.  *        argc = getredirection(argc, argv);
  470.  *    }
  471.  */
  472. {
  473.     register char        *ap;    /* Argument pointer    */
  474.     int            i;    /* argv[] index        */
  475.     int            j;    /* Output index        */
  476.     int            file;    /* File_descriptor     */
  477.     extern int        errno;    /* Last vms i/o error     */
  478.  
  479.     for (j = i = 1; i < argc; i++) {   /* Do all arguments    */
  480.         switch (*(ap = argv[i])) {
  481.         case '<':            /* <file        */
  482.         if (freopen(++ap, "r", stdin) == NULL) {
  483.             perror(ap);        /* Can't find file    */
  484.             exit(errno);    /* Is a fatal error    */
  485.         }
  486.         break;
  487.  
  488.         case '>':            /* >file or >>file    */
  489.         if (*++ap == '>') {    /* >>file        */
  490.             /*
  491.              * If the file exists, and is writable by us,
  492.              * call freopen to append to the file (using the
  493.              * file's current attributes).  Otherwise, create
  494.              * a new file with "vanilla" attributes as if the
  495.              * argument was given as ">filename".
  496.              * access(name, 2) returns zero if we can write on
  497.              * the specified file.
  498.              */
  499.             if (access(++ap, 2) == 0) {
  500.             if (freopen(ap, "a", stdout) != NULL)
  501.                 break;    /* Exit case statement    */
  502.             perror(ap);    /* Error, can't append    */
  503.             exit(errno);    /* After access test    */
  504.             }            /* If file accessable    */
  505.         }
  506.         /*
  507.          * On vms, we want to create the file using "standard"
  508.          * record attributes.  creat(...) creates the file
  509.          * using the caller's default protection mask and
  510.          * "variable length, implied carriage return"
  511.          * attributes. dup2() associates the file with stdout.
  512.          */
  513.         if ((file = creat(ap, 0, "rat=cr", "rfm=var")) == -1
  514.          || dup2(file, fileno(stdout)) == -1) {
  515.             perror(ap);        /* Can't create file    */
  516.             exit(errno);    /* is a fatal error    */
  517.         }            /* If '>' creation    */
  518.         break;            /* Exit case test    */
  519.  
  520.         default:
  521.         argv[j++] = ap;        /* Not a redirector    */
  522.         break;            /* Exit case test    */
  523.         }
  524.     }                /* For all arguments    */
  525.     argv[j] = NULL;            /* Terminate argv[]    */
  526.     return (j);            /* Return new argc    */
  527. }
  528. #endif
  529.  
  530.  
  531.